package com.joysuch.wwyt.bp.controller;


import com.joysuch.wwyt.bp.bean.inspect.BPInspectPointBean;
import com.joysuch.wwyt.bp.bean.inspect.BPInspectPointQueryBean;
import com.joysuch.wwyt.bp.entity.BPNFCLocation;
import com.joysuch.wwyt.bp.entity.inspect.BPInspectPoint;
import com.joysuch.wwyt.bp.service.BPInspectPointDetailService;
import com.joysuch.wwyt.bp.service.BPInspectPointService;
import com.joysuch.wwyt.bp.service.BpNFCLocationService;
import com.joysuch.wwyt.core.bean.BaseAddNfcCardBean;
import com.joysuch.wwyt.core.bean.BaseNfcCardBean;
import com.joysuch.wwyt.core.bean.BaseSearchNfcCardBean;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.common.util.PageDataUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j
@RestController
@RequestMapping("/bp/inspect-point")
@Api(tags = "巡检点-检查点管理")
@CrossOrigin
public class BPInspectPointController {

    @Autowired
    private BPInspectPointService inspectPointService;

    @Autowired
    private BPInspectPointDetailService inspectPointDetailService;

    @Autowired
    private BpNFCLocationService bpNFCLocationService;

    @GetMapping("/page")
    @ApiOperation(value = "01 巡检点列表(分页)", response = BPInspectPoint.class)
    public ResultBean page(Pageable pageable, BPInspectPointQueryBean queryBean) {
        pageable = PageDataUtils.addOrderByDesc(pageable, "createTime");
        Page<BPInspectPoint> pageList = inspectPointService.page(pageable, queryBean);
        return ResultBean.pageData(pageList);

    }

    @PostMapping("/save")
    @RequiresPermissions("bp:inspect-point:save")
    @ApiOperation(value = "02 保存巡检点", response = BPInspectPointBean.class)
    public ResultBean save(@RequestBody BPInspectPointBean bean) {
        return inspectPointService.save(bean);
    }


    @GetMapping("/getById/{id}")
    @ApiOperation(value = "03 根据巡检点ID获取详情", response = BPInspectPointBean.class)
    @ApiImplicitParam(name = "id", value = "巡检点ID", paramType = "path", required = true)
    public ResultBean getById(@PathVariable Long id) {
        BPInspectPointBean inspectPointBean = inspectPointService.getById(id);
        return ResultBean.success(inspectPointBean);

    }

    @GetMapping("/delete/{id}")
    @RequiresPermissions("bp:inspect-point:delete")
    @ApiOperation(value = "04 根据巡检点ID删除")
    @ApiImplicitParam(name = "id", value = "巡检点ID", paramType = "path", required = true)
    public ResultBean delete(@PathVariable Long id) {
        return inspectPointService.deleteById(id);


    }

    @GetMapping("/deleteDetail/{id}")
    @RequiresPermissions("bp:inspect-point:deleteDetail")
    @ApiOperation(value = "04 巡检点设备详情表ID删除")
    @ApiImplicitParam(name = "id", value = "设备详情表ID", paramType = "path", required = true)
    public ResultBean deleteDetail(@PathVariable Long id) {
        inspectPointDetailService.deleteById(id);
        return ResultBean.defaultSuccessResult();

    }

    @GetMapping("/nfcList")
    @ApiOperation(value = "NFC 定位列表", response = BPNFCLocation.class)
    public ResultBean nfcList() {
        List<BPNFCLocation> list = bpNFCLocationService.findAll();
        return ResultBean.success(list);

    }

    /**
     * NFC卡新增
     * @param bean
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增NFC卡")
    @RequiresPermissions("bp:inspect-point:add")
    public ResultBean add(@RequestBody BaseNfcCardBean bean) {
        try {
            BPNFCLocation card = bpNFCLocationService.add(bean);
            return ResultBean.success(card);
        } catch (Exception e) {
            log.error("创建失败", e);
            return ResultBean.fail(101, "创建失败");
        }
    }

    /**
     * NFC批量新增
     * @param bean
     * @return
     */
    @PostMapping("/addList")
    @ApiOperation(value = "批量新增NFC卡")
    @RequiresPermissions("bp:inspect-point:addList")
    public ResultBean addList(@RequestBody BaseAddNfcCardBean bean) {
        List<BPNFCLocation> list = bpNFCLocationService.addList(bean);
        return ResultBean.success(list);
    }

    /**
     * NFC卡修改
     *
     * @param bean
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改NFC卡")
    @RequiresPermissions("bp:inspect-point:update")
    public ResultBean update(@RequestBody BaseNfcCardBean bean) {
        BPNFCLocation card = bpNFCLocationService.update(bean);
        return ResultBean.success(card);
    }

    /**
     * NFC查询
     *
     * @param id
     * @return
     */
    @GetMapping("/findById")
    @ApiOperation(value = "查询NFC卡")
    public ResultBean findById(Long id) {
        try {
            BaseNfcCardBean bean = bpNFCLocationService.findById(id);
            return ResultBean.success(bean);
        } catch (Exception e) {
            log.error("查询失败", e);
            return ResultBean.fail(101, "查询失败");
        }
    }

    /**
     * 根据识别码 查询NFC
     *
     * @param code
     * @return
     */
    @GetMapping("/findByCode")
    @ApiOperation(value = "查询NFC卡")
    public ResultBean findById(String code) {
        try {
            BaseNfcCardBean bean = bpNFCLocationService.findByCode(code);
            return ResultBean.success(bean);
        } catch (Exception e) {
            log.error("查询失败", e);
            return ResultBean.fail(101, "查询失败");
        }
    }

    /**
     * NFC删除
     * @param id
     * @return
     */
    @GetMapping("/delete")
    @ApiOperation(value = "删除NFC卡")
    @RequiresPermissions("bp:inspect-point:delete")
    public ResultBean deleteCard(Long id) {
            bpNFCLocationService.deleteCard(id);
            return ResultBean.success("");
    }

    /**
     * NFC卡分页
     * @param page
     * @param condition
     * @return
     */
    @GetMapping("/pageCard")
    @ApiOperation(value = "分页查询NFC卡")
    public ResultBean page(Pageable page, BaseSearchNfcCardBean condition) {
        try {
            Page<BaseNfcCardBean> p = bpNFCLocationService.pageCard(page, condition);
            return ResultBean.pageData(p.getContent(), p.getTotalElements());
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            return ResultBean.fail(101, "分页查询失败");
        }
    }
}
